package com.websuites.web.tag;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.websuites.core.Constant;

@SuppressWarnings("unchecked")
public class Route extends TagSupport 
{

	private static final long serialVersionUID = 3265008049944376913L;
		
	private String var;
	
	public String getVar() 
	{
		return var;
	}

	public void setVar(String var) 
	{
		this.var = var;
	}

	public int doStartTag() throws JspException 
	{
		return SKIP_BODY;
	}

	public int doEndTag() throws JspException 
	{
		try
		{
			HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
			HashMap mapRoutes = (HashMap)request.getAttribute(Constant.WS_ROUTE);
		    if (mapRoutes != null)
			{
				int iCount = 0;
				pageContext.getOut().print("'" + this.var + "':{");
				Iterator itMap = mapRoutes.entrySet().iterator();
				while (itMap.hasNext())
				{
					Map.Entry element = (Map.Entry)itMap.next();
								
					String strKey = element.getKey().toString();
					String strValue = element.getValue().toString();
							
					if (iCount ++ > 0)
					{
						pageContext.getOut().print(",");
					}
					pageContext.getOut().print("'" + strKey + "':");
					pageContext.getOut().print("'" + strValue + "'");		
				}
				pageContext.getOut().println("}");
			}
		    else
		    {
		    	pageContext.getOut().println("'" + this.var + "':{}");
		    }
		}
		catch (IOException e) 
		{
			e.printStackTrace();
		}
								
		return EVAL_PAGE;
	}
}
